
use std::fs::{read_to_string};
use std::fmt;

#[derive(Debug)]
enum MyError {
    EnvVarNotFound,
    IOError(std::io::Error),
}

impl From<std::env::VarError> for MyError {
    fn from(_e: std::env::VarError) -> Self{
        Self::EnvVarNotFound
    }
}

impl From<std::io::Error> for MyError {
    fn from(e: std::io::Error) -> Self{
        Self::IOError(e)
    }
}

impl std::error::Error for MyError {}

impl fmt::Display for MyError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            MyError::EnvVarNotFound => write!(f, "env var not found"),
            MyError::IOError(e) => write!(f, "io error: {}", e.to_string()),
        }
    }
}

fn main() -> Result<(), MyError> {
    // println!("Hello, world!");
    let html = render()?;
    println!("{html}");

    Ok(())
}

fn render() -> Result<String, MyError> {
    let fp = std::env::var("MARKDOWN")?;
    let content = read_to_string(fp)?;

    Ok(content)
}
